// Quantitative Angiogram Analysis System
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace eee.Sheffield.PZ.QAAS
{
    /// <summary>
    /// QAAS configuration
    /// </summary>
    public class Configuration
    {
        #region Fields
        public bool autoSaveLog = true;
        public bool showLogForm = false;
        public bool showHistogramForm = true;
        public bool showFilterForm = true;
        #endregion

        #region Methods
        /// <summary>
        /// load configuration
        /// </summary>
        /// <param name="fileName"></param>
        public void Load(string fileName)
        {
            if (File.Exists(fileName))
            {
                FileStream fs = null;
                XmlTextReader xmlIn = null;

                try
                {
                    // open file
                    fs = new FileStream(fileName, FileMode.Open);
                    // create XML reader
                    xmlIn = new XmlTextReader(fs);

                    xmlIn.WhitespaceHandling = WhitespaceHandling.None;
                    xmlIn.MoveToContent();

                    // check for main node
                    if (xmlIn.Name != "QAASConfiguration")
                        throw new ApplicationException("Configuration::Load(), Cannot find QAASConfiguration node.");

                    // move to next node
                    xmlIn.Read();
                    if (xmlIn.NodeType == XmlNodeType.EndElement)
                        xmlIn.Read();

                    // check for position node
                    if (xmlIn.Name != "Settings")
                        throw new ApplicationException("Configuration::Load(), Cannot find Settings node.");
                    autoSaveLog = Convert.ToBoolean(xmlIn.GetAttribute("autoSaveLog"));
                    showLogForm = Convert.ToBoolean(xmlIn.GetAttribute("showLogForm"));
                    showHistogramForm = Convert.ToBoolean(xmlIn.GetAttribute("showHistogramForm"));
                    showFilterForm = Convert.ToBoolean(xmlIn.GetAttribute("showFilterForm"));
                }
                catch
                {
                }
                finally
                {
                    if (xmlIn != null)
                        xmlIn.Close();
                }
            }
        }

        /// <summary>
        /// save configuration
        /// </summary>
        /// <param name="fileName"></param>
        public void Save(string fileName)
        {
            // open file
            FileStream fs = new FileStream(fileName, FileMode.Create);
            // create XML writer
            XmlTextWriter xmlOut = new XmlTextWriter(fs, Encoding.Unicode);

            // use indenting for readability
            xmlOut.Formatting = Formatting.Indented;

            // start document
            xmlOut.WriteStartDocument();
            xmlOut.WriteComment("QAAS Configuration File");

            // main node
            xmlOut.WriteStartElement("QAASConfiguration");

            // setting node
            xmlOut.WriteStartElement("Settings");
            xmlOut.WriteAttributeString("autoSaveLog", autoSaveLog.ToString());
            xmlOut.WriteAttributeString("showLogForm", showLogForm.ToString());
            xmlOut.WriteAttributeString("showHistogramForm", showHistogramForm.ToString());
            xmlOut.WriteAttributeString("showFilterForm", showFilterForm.ToString());
            xmlOut.WriteEndElement();

            xmlOut.WriteEndElement();
            xmlOut.WriteEndDocument();
            // close file
            xmlOut.Close();
        }
        #endregion
    }
}
